package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.common.UrlConfig;
import com.wanlian.entity.Device;
import com.wanlian.entity.DeviceConfig;
import com.wanlian.entity.Storage;
import com.wanlian.entity.User;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.netty.msg.Device.model.Disk;
import com.wanlian.service.ConfigServices;
import com.wanlian.service.DeviceServices;
import com.wanlian.service.StorageServices;
import com.wanlian.util.HttpUtils;
import com.wanlian.util.MacAddressUtil;
import com.wanlian.vo.DeviceUpGradeVo;
import com.wanlian.vo.DeviceVo;
import com.wanlian.vo.VersionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    public DeviceServices deviceServices;

    @Autowired
    private StorageServices storageServices;

    @Autowired
    public ConfigServices configServices;

    @Autowired
    public UrlConfig urlConfig;


    /**
     * 运营平台
     * 按条件查询设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = deviceServices.queryDevice(conditions, 0);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台
     * 按条件查询设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByConditionApp(@RequestBody String data, HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = userSessionInfo.getLoginUser().getId();
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = deviceServices.queryDevice(conditions, userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * Web平台
     * 查看用户关联设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByUserId(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        if (conditions.isExistByKey("userId")) {
            int userId = (Integer) conditions.getByKey("userId");
            ResultModel resultModel = deviceServices.queryDeviceByUser(conditions.getPage(), userId);
            return new JsonResult().Serailze(resultModel);
        } else {
            ResultModel resultModel = new ResultModel();
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.DEVICE_NOTFINDUID);
            return new JsonResult().Serailze(resultModel);
        }
    }

    /**
     * 、
     * App平台
     * 查看用户关联设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/list/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByUserIdApp(@RequestBody String data, HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        if (userId != 0) {
            ResultModel resultModel = deviceServices.queryDeviceByUser(conditions.getPage(), userId);
            return new JsonResult().Serailze(resultModel);
        } else {
            ResultModel resultModel = new ResultModel();
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.DEVICE_NOTFINDUID);
            return new JsonResult().Serailze(resultModel);
        }
    }

    /**
     * 添加设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        device.setDeviceName("天翼链路由器");
        ResultModel resultModel = deviceServices.doAdd(device);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 添加设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add/patch", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String patchAdd(@RequestBody String data) {
        List<Device> device = JsonStringUtil.readValueByList(data, Device.class);
        ResultModel resultModel = deviceServices.doAdd(device);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 批量修改线上线下设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/upd/patch", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String patchUpd(@RequestBody String data) {
        List<Device> device = JsonStringUtil.readValueByList(data, Device.class);
        ResultModel resultModel = deviceServices.doUpd(device);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 编辑设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String edit(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.doedit(device);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * web
     * 查找设备详情
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/info", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryByDevice(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.queryDeviceInfo(device);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * app
     * 查找设备详情
     *
     * @return
     */
    @RequestMapping(value = "/app/info", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryByDeviceApp(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = userSessionInfo.getLoginUser().getId();
        ResultModel resultModel = deviceServices.queryDeviceInfoApp(userId);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 删除设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String deleteById(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.doDeleteById(device.getId());
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 启用设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String enableById(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.doEnableById(device.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台
     * 绑定设备:通过扫描二维码bind
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/bind", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String bindByDeviceAndUser(@RequestBody String data) {
        ResultModel resultModel = new ResultModel();
        try {
            DeviceVo deviceVO = JsonStringUtil.readValue(data, DeviceVo.class);
            if (deviceVO.getDevice() != null && deviceVO.getUser() != null) {
                Device device = deviceVO.getDevice();
                User user = deviceVO.getUser();
                int userId = user.getId();
                int macLength = device.getMacAddress().length();
                String macAddress = "";
                String deviceKey = "";
                String deviceId="";
                if (macLength == 12) {
                    macAddress = MacAddressUtil.tidyMac(device.getMacAddress());
                    Device devicedb= deviceServices.queryByMac(macAddress);
                    deviceKey=devicedb.getDevicekey();
                    deviceId=devicedb.getDeviceId();
                } else {
                    macAddress=device.getMacAddress();
                    Device devicedb= deviceServices.queryByMac(macAddress);
                    deviceKey=devicedb.getDevicekey();
                    deviceId=devicedb.getDeviceId();
                }
                resultModel = deviceServices.doBindDeviceByDeviceKey(deviceKey, userId);
                if (resultModel.isBresult()) {
                    ResultModel resultModel1 = storageServices.queryStorageByUserId(userId, deviceId);
                    if (resultModel1.getObject() != null) {
                        System.out.println(resultModel1.getObject());
                        resultModel.setMessage(ErrorCode.STORAGE_USER_EXISTS);
                    } else {
                        device.setUserid(userId);
                        device.setDeviceId(deviceId);
                        this.storageServices.doAdd(device);
                    }
                }
                return new JsonResult().Serailze(resultModel);
            } else {
                resultModel.setMessage(ErrorCode.DEVICE_NOTFINDKey);
                resultModel.setBresult(false);
                return new JsonResult().Serailze(resultModel);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            resultModel.setMessage(ErrorCode.DEVICE_NOTFINDKey);
            resultModel.setBresult(false);
            return new JsonResult().Serailze(resultModel);
        }
    }

    /**
     * App端
     * 绑定设备:通过扫描二维码bind
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/bind", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String bindByDeviceAndUserApp(@RequestBody String data, HttpSession session) {
        ResultModel resultModel = new ResultModel();
        try {
            UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
            if (user == null) {
                return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
            }
            int userId = user.getLoginUser().getId();
            Device device = JsonStringUtil.readValue(data, Device.class);
            if (device != null) {
                int macLength = device.getMacAddress().length();
                String macAddress = "";
                String deviceKey = "";
                if (macLength == 12) {
                    macAddress = MacAddressUtil.tidyMac(device.getMacAddress());
                    device=deviceServices.queryByMac(macAddress);
                    deviceKey = device.getDevicekey();
                } else {
                    if(macLength==17) {
                        macAddress=device.getMacAddress();
                        device=deviceServices.queryByMac(macAddress);
                        deviceKey = device.getDevicekey();
                    }
                    else{
                        deviceKey= device.getMacAddress();
                        device=deviceServices.queryByDeviceKey(deviceKey);
                    }
                }
                device.setDevicekey(deviceKey);
                device.setUserid(userId);
                resultModel = deviceServices.doBindDeviceByDeviceKey(deviceKey, userId);
                if (resultModel.isBresult()) {
                    ResultModel resultModel1 = storageServices.queryStorageByUserId(userId, device.getDeviceId());
                    if (resultModel1.getObject() != null) {
                        resultModel.setMessage(ErrorCode.STORAGE_USER_EXISTS);
                    } else {
                        this.storageServices.doAdd(device);
                    }
                }
                return new JsonResult().Serailze(resultModel);
            } else {
                resultModel.setMessage(ErrorCode.DEVICE_NOTFINDKey);
                resultModel.setBresult(false);
                return new JsonResult().Serailze(resultModel);
            }
        }
        catch (Exception e){
            resultModel.setMessage(ErrorCode.DEVICE_NOTFINDKey);
            resultModel.setBresult(false);
            return new JsonResult().Serailze(resultModel);
        }
    }


    /**
     * 查询某台设备的积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryScoreByDeviceAndPage(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        Integer id = Integer.valueOf(pageCondition.getByKey("id").toString());
        ResultModel resultModel = deviceServices.queryScoreHistoryByDeviceId(pageCondition.getPage(), id);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 查询某台设备的昨日积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score/list/yes", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryYesScoreByDeviceAndPage(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        Integer id = Integer.valueOf(pageCondition.getByKey("id").toString());
        ResultModel resultModel = deviceServices.queryScoreHistoryByDeviceId(pageCondition.getPage(), id);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台
     * 查询所有设备的积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score/list/all", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryDeviceScore(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = deviceServices.queryDeviceScoreHistoryByParam(pageCondition, 0);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台
     * 查询设备的积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/score/list/all", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryDeviceScoreApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = deviceServices.queryDeviceScoreHistoryByParam(pageCondition, userId);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 添加设备配置
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/config/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addConfig(@RequestBody String data) {
        DeviceConfig deviceConfig = JsonStringUtil.readValue(data, DeviceConfig.class);
        ResultModel resultModel = deviceServices.doAddDeviceConfig(deviceConfig);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改设备配置
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/config/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyConfig(@RequestBody String data, HttpSession session) {
        DeviceConfig deviceConfig = JsonStringUtil.readValue(data, DeviceConfig.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = deviceServices.doEditDeviceConfig(deviceConfig, userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 拨号连接
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/config/call", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyConfigCall(@RequestBody String data, HttpSession session) {
        DeviceConfig deviceConfig = JsonStringUtil.readValue(data, DeviceConfig.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = deviceServices.doEditDeviceConfigCall(deviceConfig, userId);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 解除绑定web
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/web/untied", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String midifyUntied(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        int userId = 0;
        ResultModel resultModel = deviceServices.doEditDevice(device, userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 解除绑定app
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/untied", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String midifyUntied(@RequestBody String data, HttpSession session) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = deviceServices.doEditDevice(device, userId);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * web
     * 查询用户设备台数
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/count", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserDeviceCount(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = deviceServices.queryUserDeviceCount(user.getId());
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * App
     * 查询用户设备台数
     *
     * @return
     */
    @RequestMapping(value = "/app/user/count", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserDeviceCountApp(HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = deviceServices.queryUserDeviceCount(userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 切换使用设备
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/set/current", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String switchDevice(@RequestBody String data, HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(new ResultModel(false, ErrorCode.LOGIN_USER));
        }
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.doSetDeviceCurrent(device.getDeviceId(), userSessionInfo.getLoginUser().getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询当前设备
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/get/currentdevice", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String getCDevice(HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        Device device = deviceServices.queryDeviceByUserId(userId);
        ResultModel resultModel = new ResultModel();
        resultModel.setObject(device);
        resultModel.setBresult(true);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 进入设置页面加载逆袭
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/get/setinfo", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String getInfo(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(new ResultModel(false, ErrorCode.LOGIN_USER));
        }
        int userId = userSessionInfo.getLoginUser().getId();
        ResultModel resultModel = deviceServices.queryConfigInfo(userId);
        return new JsonResult().Serailze(resultModel);
    }


    @RequestMapping(value = "/app/setmemory", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryByDevice(@RequestBody String data, HttpSession session) {
        Storage storage = JsonStringUtil.readValue(data, Storage.class);
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(new ResultModel(false, ErrorCode.LOGIN_USER));
        }
        int userId = userSessionInfo.getLoginUser().getId();
        ResultModel resultModel = deviceServices.doEditDeviceMemoryConfig(storage, userId);
        return new JsonResult().Serailze(resultModel);
    }

    @RequestMapping(value = "/app/getmemory", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryByDeviceStorage(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo == null) {
            return new JsonResult().Serailze(new ResultModel(false, ErrorCode.LOGIN_USER));
        }
        int userId = userSessionInfo.getLoginUser().getId();
        ResultModel resultModel = deviceServices.queryDeviceMemoryConfig(userId);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 修改设备为在线状态
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/updonline", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String updOnline(@RequestBody String data) {
        Device device = JsonStringUtil.readValue(data, Device.class);
        ResultModel resultModel = deviceServices.doEditOnline(device);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改设备为在线状态批量
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/updonline/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String updOnlineBatch(@RequestBody String data) {
        List<Device> devices = JsonStringUtil.readValueByList(data, Device.class);
        ResultModel resultModel = deviceServices.editOnlineBatch(devices);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 固件升级单台
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/force/upgradeone", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String upgradeOne(@RequestBody String data) {
        ResultModel resultModel = configServices.queryConfigByKey("LUCI_VERSION");
        String url = urlConfig.getUpgradeone();
        try {
            Device device = JsonStringUtil.readValue(data, Device.class);
            VersionVo versionVo = (VersionVo) resultModel.getObject();
            DeviceUpGradeVo deviceUpGradeVo = new DeviceUpGradeVo(versionVo.getVersionCode(), versionVo.getMd5(), versionVo.getDownUrl(), device.getDevicekey());
            HttpUtils.sendPost(url, JsonStringUtil.writeValueAsString(deviceUpGradeVo));
        } catch (Exception e) {

        }
        resultModel.setMessage("升级指令已发送");
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 固件升级全部
     *
     * @return
     */
    @RequestMapping(value = "/force/upgrade", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String upgrade() {
        ResultModel resultModel = configServices.queryConfigByKey("LUCI_VERSION");
        String url = urlConfig.getUpgrade();
        try {
            VersionVo versionVo = (VersionVo) resultModel.getObject();
            DeviceUpGradeVo deviceUpGradeVo = new DeviceUpGradeVo();
            deviceUpGradeVo.setMd5(versionVo.getMd5());
            deviceUpGradeVo.setDownUrl(versionVo.getDownUrl());
            deviceUpGradeVo.setVersionCode(versionVo.getVersionCode());
            List<String> keys = new ArrayList<>();
            for (Device device : deviceServices.queryDevicesOnline()) {
                keys.add(device.getDevicekey());
            }
            deviceUpGradeVo.setKeys(keys);
            HttpUtils.sendPost(url, JsonStringUtil.writeValueAsString(deviceUpGradeVo));
        } catch (Exception e) {

        }
        resultModel.setMessage("升级指令已发送");
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改设备共享空间
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/dsharesize", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyShareSize(@RequestBody String data) {
        Disk disk = JsonStringUtil.readValue(data, Disk.class);
        ResultModel resultModel = deviceServices.doEditStorage(disk);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 设置所有设备为离线
     *
     * @return
     */
    @RequestMapping(value = "/offlineall", method = {RequestMethod.GET})
    @ResponseBody
    public String offlineAll() {
        ResultModel resultModel = deviceServices.editOfflineAll();
        return new JsonResult().Serailze(resultModel);
    }


}
